package com.atguigu.project;

import com.atguigu.been.UserBehavior;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.MapPartitionFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.aggregation.AggregationFunction;
import org.apache.flink.util.Collector;
import org.elasticsearch.common.util.IntArray;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @author wky
 * @create 2021-07-16-9:41
 */
public class Project_UV {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment senv = StreamExecutionEnvironment.getExecutionEnvironment();
        senv.setParallelism(1);
        //senv.setRuntimeMode(RuntimeExecutionMode.BATCH);
        DataStreamSource<String> streamSource = senv.readTextFile("src/input/UserBehavior.csv");
        SingleOutputStreamOperator<UserBehavior> map = streamSource.map(new MapFunction<String, UserBehavior>() {
            @Override
            public UserBehavior map(String s) throws Exception {
                String[] split = s.split(",");
                return new UserBehavior(
                        Long.parseLong(split[0]),
                        Long.parseLong(split[1]),
                        Integer.parseInt(split[2]),
                        split[3],
                        Long.parseLong(split[4])
                );
            }
        });
        SingleOutputStreamOperator<UserBehavior> filter = map.filter(new FilterFunction<UserBehavior>() {
            @Override
            public boolean filter(UserBehavior userBehavior) throws Exception {
                return "pv".equals(userBehavior.getBehavior());
            }
        });
        map.process(new ProcessFunction<UserBehavior, Map<String,Integer>>() {
            HashSet<Long> strings = new HashSet<>();
            HashMap<String, Integer> integerHashMap = new HashMap<>();
            @Override
            public void processElement(UserBehavior userBehavior, Context context, Collector<Map<String, Integer>> collector) throws Exception {
                strings.add(userBehavior.getUserId());
                integerHashMap.put("uv",strings.size());
                collector.collect(integerHashMap);

            }
        }).print();
        senv.execute();


    }
}
